#ifndef XMLCONFIG_H
#define XMLCONFIG_H

#include <QtXml/QDomDocument>

#include "hbot.h"

// Error codes

#define XMLCONFIG_ERROR_SUCCESS							0
#define XMLCONFIG_ERROR_OPENREAD						1
#define XMLCONFIG_ERROR_SETCONTENT						2
#define XMLCONFIG_ERROR_INVALIDTAGNAME					3
#define XMLCONFIG_ERROR_OPENWRITE						4
#define XMLCONFIG_ERROR_DELETEPROFILE					5
#define XMLCONFIG_ERROR_EMPTYPROFILE					6
#define XMLCONFIG_ERROR_EMPTYGROUP						7
#define XMLCONFIG_ERROR_EMPTYVALUEELEMENT				8
#define XMLCONFIG_ERROR_EMPTYVALUE						9
#define XMLCONFIG_ERROR_INVALIDPROFILE					10
#define XMLCONFIG_ERROR_PROFILENONEXISTENT				11
#define XMLCONFIG_ERROR_PROFILETAGNONEXISTENT			12

#define XMLCONFIG_ERROR_FUNCTION_GETVALUE				"GetProfileValue(const QString &profile, const QString &group, const QString &valueElement, const QString &attrib)"
#define XMLCONFIG_ERROR_FUNCTION_SETVALUE				"SetProfileValue(const QString &profile, const QString &group, const QString &valueElement, const QString &value, const QString &attrib)"

// Profile File Location

#define PROFILE_XML_FILEDIR								"D:/World-of-Warcraft/HBot-repo/HBot/Data/"
#define PROFILE_XML_FILENAME							"profiles.xml"
#define PROFILE_XML_FILEFULL							PROFILE_XML_FILEDIR PROFILE_XML_FILENAME
#define PROFILE_XML_ROOT_TAGNAME						"profiles"

// Tags

#define TAG_PROFILE_PROFILE								"profile"
#define TAG_PROFILE_PROFILE_LOGFILE						"log_file"
#define TAG_PROFILE_PROFILE_LOGMETHOD					"log_method"
#define TAG_PROFILE_PROFILE_RUNONOPEN					"run_on_open"
#define TAG_PROFILE_PROFILE_USINGTFT					"using_tft"
#define TAG_PROFILE_BATTLENET							"battle.net-"
#define TAG_PROFILE_BATTLENET_1							"battle.net-1"
#define TAG_PROFILE_BATTLENET_ENABLED					"enabled"
#define TAG_PROFILE_BATTLENET_SERVER					"server"
#define TAG_PROFILE_BATTLENET_CDKEY						"cdkey"
#define TAG_PROFILE_BATTLENET_LOCALE					"locale"
#define TAG_PROFILE_BATTLENET_LOGIN						"login"
#define TAG_PROFILE_BATTLENET_CHANNEL					"first_channel"
#define TAG_PROFILE_BATTLENET_ROOTADMIN					"root_admin"
#define TAG_PROFILE_BATTLENET_COMMANDTRIGGER			"command_trigger"
#define TAG_PROFILE_BATTLENET_HOLDFRIENDS				"hold_friends"
#define TAG_PROFILE_BATTLENET_HOLDCLAN					"hold_clan"
#define TAG_PROFILE_BATTLENET_PUBLICCOMMANDS			"public_commands"
#define TAG_PROFILE_BATTLENET_PVPGN						"pvpgn"
#define TAG_PROFILE_DATABASE							"database"
#define TAG_PROFILE_DATABASE_TYPE						"type"
#define TAG_PROFILE_DATABASE_SQLITE						"sqlite"
#define TAG_PROFILE_DATABASE_MYSQL						"mysql"

// Attributes

#define ATTRIB_PROFILE_PROFILE_NAME						"name"
#define ATTRIB_PROFILE_BATTLENET_SERVER_NAME			"name"
#define ATTRIB_PROFILE_BATTLENET_SERVER_ALIAS			"alias"
#define ATTRIB_PROFILE_BATTLENET_CDKEY_ROC				"roc"
#define ATTRIB_PROFILE_BATTLENET_CDKEY_TFT				"tft"
#define ATTRIB_PROFILE_BATTLENET_COUNTRY_CODE			"country"
#define ATTRIB_PROFILE_BATTLENET_LANGUAGE_CODE			"language"
#define ATTRIB_PROFILE_BATTLENET_LOGIN_USERNAME			"username"
#define ATTRIB_PROFILE_BATTLENET_LOGIN_PASSWORD			"password"
#define ATTRIB_PROFILE_BATTLENET_CHANNEL_NAME			"name"
#define ATTRIB_PROFILE_BATTLENET_ROOTADMIN_AMOUNT		"amount"
#define ATTRIB_PROFILE_BATTLENET_ROOTADMIN				"ra-"
#define ATTRIB_PROFILE_BATTLENET_COMMANDTRIGGER			"trigger"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_ENABLED			"enabled"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_W3VERSION		"w3version"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_EXEVERSION		"exeversion"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_EXEVERSIONHASH	"exeversionhash"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_PASSWORDHASHTYPE	"passwordhashtype"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_REALMNAME		"realmname"
#define ATTRIB_PROFILE_BATTLENET_PVPGN_MAXMESSAGELENGTH	"maxmessagelength"
#define ATTRIB_PROFILE_DATABASE_NAME					"name"
#define ATTRIB_PROFILE_DATABASE_SQLITE_FILENAME			"filename"
#define ATTRIB_PROFILE_DATABASE_MYSQL_SERVER			"server"
#define ATTRIB_PROFILE_DATABASE_MYSQL_DATABASE			"database"
#define ATTRIB_PROFILE_DATABASE_MYSQL_USER				"user"
#define ATTRIB_PROFILE_DATABASE_MYSQL_PASSWORD			"password"
#define ATTRIB_PROFILE_DATABASE_MYSQL_PORT				"port"

// Misc.

#define INDENT_SIZE										4

class CXMLConfig
{
private:
	int m_ErrorColumn;
	int m_ErrorLine;
	int m_ErrorXML;

	QDomDocument m_DomDocument;
	QDomElement m_RootElement;
	QDomNode m_PreviousProfile;
	QString m_ErrorString;
	QString m_FileName;

public:
	CXMLConfig(const QString &fileName);
	~CXMLConfig();
	
	void ChangeProfileName(const QString &currentName, const QString &changedName);
	void CreateProfile(const QString &name);
	void DeleteProfile(const QString &name);
	void ErrorCheck();
	void Read();
	void SetDefaultProfile(const QString &profile);
	void SetProfileValue(const QString &profile, const QString &group, const QString &valueElement, const QString &value, const QString &attrib = QString());
	void Write();
	QString GetDefaultProfile() const;
	QString GetProfileValue(const QString &defaultVal, const QString &profile, const QString &group, const QString &valueElement, const QString &attrib = QString());
	QStringList GetProfiles() const;
	QVector<int> GetNumberBnet(const QString &profile);

	const int GetErrorColumn() const													{ return m_ErrorColumn; }
	const int GetErrorLine() const														{ return m_ErrorLine; }
	const int GetErrorXML() const														{ return m_ErrorXML; }
	const QString GetErrorString() const												{ return m_ErrorString; }
};

#endif /* XMLCONFIG_H */
